#include "StdAfx.h"
#include "GII.h"

GII::GII(HDC hdc)
{
	m_hdc = hdc;
}

GII::~GII(void)
{
}

void GII::Point(int x, int y, COLORREF cl)
{
	SetPixel(m_hdc, x, y, cl);
}

void GII::Point(SPoint pt, COLORREF cl )
{
	SetPixel(m_hdc, pt.x, pt.y, cl);
}

void GII::Line(int x1, int y1, int x2, int y2)
{
	MoveToEx(m_hdc, x1, y1, NULL);  
	LineTo(m_hdc, x2, y2);
}

void GII::Line(SPoint pt1, SPoint pt2)
{
	MoveToEx(m_hdc, pt1.x, pt1.y, NULL);  
	LineTo(m_hdc, pt2.x, pt2.y);
}

void GII::Rectangle(SRect rc)
{
	MoveToEx(m_hdc, rc.left, rc.top, NULL);  
	LineTo(m_hdc, rc.left, rc.bottom);
	MoveToEx(m_hdc, rc.left, rc.bottom, NULL);  
	LineTo(m_hdc, rc.right, rc.bottom);
	MoveToEx(m_hdc, rc.right, rc.bottom, NULL);  
	LineTo(m_hdc, rc.right, rc.top);
	MoveToEx(m_hdc, rc.right, rc.top, NULL);  
	LineTo(m_hdc, rc.left, rc.top);
}

void GII::Rectangle(int x1, int y1, int x2, int y2)
{
	MoveToEx(m_hdc, x1, y1, NULL);  
	LineTo(m_hdc, x1, y2);
	MoveToEx(m_hdc, x1, y2, NULL);  
	LineTo(m_hdc, x2, y2);
	MoveToEx(m_hdc, x2, y2, NULL);  
	LineTo(m_hdc, x2, y1);
	MoveToEx(m_hdc, x2, y1, NULL);  
	LineTo(m_hdc, x1, y1);
}

void GII::Circle(int x, int y)
{
	SEllipse(m_hdc, x, y, x, y);
}

void GII::Ellipse(int x1, int y1, int x2, int y2)
{
	SEllipse(m_hdc, x1, y1, x2, y2);
}

void GII::Text(int x, int y, SString str, int nHoriType, int nVertType)
{
	 SIZE tSize = {0};
	 GetTextExtentPoint32A(m_hdc, str.GetData(), str.GetLength(), &tSize);

	 if (nHoriType == HORI_CENTER)
	 {
		 SetTextAlign(m_hdc, TA_CENTER);
	 }
	 else if (nHoriType == HORI_LEFT)
	 {
		 SetTextAlign(m_hdc, TA_LEFT);
	 }
	 else if (nHoriType == HORI_RIGHT)
	 {
		 SetTextAlign(m_hdc, TA_RIGHT);
	 }

	 if (nVertType == VERT_CENTER)
	 {
		 y -= int(tSize.cy/2);
	 }
	 else if (nVertType == VERT_BOTTOM)
	 {
		 y -= (int)tSize.cy;
	 }	 

	TextOut(m_hdc, x, y, str.GetData(), str.GetLength());
}

void GII::Text(SRect rc, SString str, int nHoriType, int nVertType)
{
	SIZE tSize = {0};
	GetTextExtentPoint32A(m_hdc, str.GetData(), str.GetLength(), &tSize);

	int x = 0, y = 0;

	if (nHoriType == HORI_LEFT)
	{
		x = rc.left;
		SetTextAlign(m_hdc, TA_LEFT);
	}
	else if (nHoriType == HORI_CENTER)
	{
		x = rc.CenterPoint().x;
		SetTextAlign(m_hdc, TA_CENTER);
	}
	else if (nHoriType == HORI_RIGHT)
	{
		x = rc.right;
		SetTextAlign(m_hdc, TA_RIGHT);
	}

	if (nVertType == VERT_TOP)
	{
		y = rc.top;
	}
	else if (nVertType == VERT_CENTER)
	{
		y = rc.CenterPoint().y - tSize.cy/2;
	}
	else if (nVertType == VERT_BOTTOM)
	{
		y = rc.bottom - tSize.cy;
	}

	TextOut(m_hdc, x, y, str.GetData(), str.GetLength());
}

void GII::FillRect(SRect rc, SBrush * br)
{
	RECT rc1 = rc.ToRECT();
	SFillRect(m_hdc, &rc1, br->GetBrush());
}

void GII::FillRect(int x, int y, int width, int height, SBrush * br)
{
	SRect rc(x, y, x + width, y + height);
	RECT rc1 = rc.ToRECT();
	SFillRect(m_hdc, &rc1, br->GetBrush());
}

SRect GII::GetAppRect() const
{
	RECT rc;
	GetClientRect(WindowFromDC(m_hdc), &rc);
	return SRect(rc);
}

SRect GII::GetLocRect() const
{
	RECT rc;
	GetWindowRect(WindowFromDC(m_hdc), &rc);
	return SRect(rc);
}

SRect GII::App2Loc(SWin * pWin, SRect rc)
{
	SRect rc1 = pWin->GetRect();
	return SRect(rc.left + rc1.left, rc.top + rc1.top, rc.right + rc1.left, rc.bottom + rc1.top);
}

SRect GII::Loc2App(SWin * pWin, SRect rc)
{
	SRect rc1 = pWin->GetRect();
	return SRect(rc.left - rc1.left, rc.top - rc1.top, rc.right - rc1.left, rc.bottom - rc1.top);
}

void GII::SetTextBkColor(COLORREF cl)
{
	SetBkColor(m_hdc, cl);
}

void GII::SetTextColor(COLORREF cl)
{
	SSetTextColor(m_hdc, cl);
}

void GII::SelectPen(SPen * pen)
{
	SelectObject(m_hdc,pen->GetPen());
}

void GII::SelectFont(SFont * font)
{
	SelectObject(m_hdc, font->GetFont());
}

void GII::SetBkMode(int mode)
{
	SSetBkMode(m_hdc, mode);
}